﻿var Id;
$(function () {
    Id = $("#hdnId").val();

    $("#nowLevel").ligerComboBox({
        cancelable: false,
        width: 110,
        valueFieldID: 'hdnNowLevel'
    });

    $("#applyLevel").ligerComboBox({
        cancelable: false,
        width: 110,
        valueFieldID: 'hdnApplyLevel'
    });

    $("#btnBack").click(function () {
        if ($("#hdnPageType").val() == 0) {
            location.href = "/Personnel/NurseLevelList";
        }
        else {
            location.href = "/Personnel/NurseLevelReview";
        }
    });

    bindNurseLevel();
});

function bindNurseLevel() {
    if (Id != 0) {
        $.ajax({
            url: "/Personnel/EditNurseLevel/GetNurseLevelInfo?ram" + Math.random(),
            type: "post",
            data: { id: Id },
            success: function (data) {
                var d = $.parseJSON(data);
                if (d.Result == 1) {
                    var nurseLevel = d.Body;
                    if (nurseLevel.Status != $("#hdnStatus").val() && Id != 0) {
                        $.ligerDialog.error("申请状态已改变！");
                        setTimeout(function () { $("#btnBack").click(); }, 1000);
                        return;
                    }
                    else
                    {
                        getStatusName(nurseLevel.Status);
                    }
                    $(".reviewInfo").show();
                    Id = nurseLevel.Id;
                    $("#hdnId").val(Id);
                    $("#fontName").text(nurseLevel.UserName);
                    $("#fontDepartmentName").text(nurseLevel.DepartmentName);
                    $("#fontInDate").text(nurseLevel.InDate == null ? "" : nurseLevel.InDate);
                    $("#fontBirthday").text(nurseLevel.birthday == null ? "" : nurseLevel.birthday);
                    var nowLevel = nurseLevel.NowLevel;
                    if (nowLevel == null) {
                        nowLevel = nurseLevel.EnergyLevel
                    }
                    $("#nowLevel").ligerComboBox().setValue(nowLevel);
                    $("#applyLevel").ligerComboBox().setValue(nurseLevel.ApplyLevel);
                    $("#applyLevel_txt").blur();

                    if ((nurseLevel.ReviewReason == null || nurseLevel.ReviewReason == "") && (nurseLevel.ReviewReason1 == null || nurseLevel.ReviewReason1 == "")) {
                        $("[tab='reviewReason']").hide();
                    }
                    else {
                        if (nurseLevel.ReviewReason != null && nurseLevel.ReviewReason != "") {
                            $("#fontReviewReason").text("护士长审核：" + nurseLevel.ReviewReason);
                        }
                        if (nurseLevel.ReviewReason1 != null && nurseLevel.ReviewReason1 != "") {
                            $("#fontReviewReason1").text("护理部审核：" + nurseLevel.ReviewReason1);
                        }
                    }
                    if (nurseLevel.ReviewReason == null) {
                        $("[tab='commitTime']").hide();
                    }
                    $("#fontCommitTime").text(nurseLevel.CommitTime);
                    $("#txtRemarks").val(nurseLevel.Remarks);

                    if ($("#hdnPageType").val() == 0) {
                        if (nurseLevel.Status == 0 || nurseLevel.Status == 3 || nurseLevel.Status == 5) {
                            $("#btn1").val("暂存").show().click(function () { save(nurseLevel.Status) });
                            $("#btn2").val("提交").show().click(function () { save(1) });
                        }
                    }
                    else if ($("#hdnPageType").val() == 1) {
                        var detailsViewBtns = JSON.parse($("#hdnDetailsViewBtns").val().replace(/([^\\])'/g, '$1\"'));
                        for (var i = 0; i < detailsViewBtns.length; i++) {
                            var detailsViewBtn = detailsViewBtns[i];
                            var run = detailsViewBtn.Function + '(' + detailsViewBtn.Parameter + ')';
                            $("#btn" + (i + 1)).val(detailsViewBtn.BtnName).attr("run", run).show().click(function () { eval($(this).attr("run")) });
                        }
                    }
                }
                else if (d.Result == 0) {
                    $.ligerDialog.error(d.Body);
                }
                else {
                    $.ligerDialog.error("程序错误");
                }
            },
            error: function () {
                $.ligerDialog.error("程序错误");
            }
        });
    }
    else {
        $.ajax({
            url: "/Personnel/EditNurseLevel/GetPersonnelUserID?ram" + Math.random(),
            type: "post",
            async: false,
            success: function (r) {
                var d = $.parseJSON(r);
                $("#fontName").text(d.p.UserName);
                $("#fontDepartmentName").text(d.departmentName);
                $("#fontInDate").text(d.p.InDate == null ? "" : d.p.InDate);
                $("#fontBirthday").text(d.p.Birthday == null ? "" : d.p.Birthday);
                $("#nowLevel").ligerComboBox().setValue(d.p.EnergyLevel);
                $("#applyLevel").ligerComboBox().setValue(-1);
                $("#applyLevel_txt").blur();
            }
        });

        $("#btn1").val("暂存").show().click(function () { save(0) });
        $("#btn2").val("提交").show().click(function () { save(1) });
    }
}

function getStatusName(status) {
    $.ajax({
        url: "/Personnel/NurseLevelReview/GetReviewProcess?ram" + Math.random(),
        type: "post",
        async: false,
        success: function (r) {
            var job = $.parseJSON(r);
            if (job.IncludeStatus == "" || job.IncludeStatus == null) return;
            var includeStatus = job.IncludeStatus.split('|');
            var statusAlias = job.StatusAlias.split('|');
            for (var i = 0; i < includeStatus.length; i++) {
                if (includeStatus[i] == status) {
                    $("#fontStatus").text(statusAlias[i]);
                    break;
                }
            }
        }
    });
}

function save(status) {
    var applyLevel = $("#hdnApplyLevel").val();
    var nowLevel = $("#hdnNowLevel").val();
    var remarks = $("#txtRemarks").val();
    if (nowLevel == "") nowLevel = "0";
    if (applyLevel == "-1") {
        $.ligerDialog.error("申请能级不能为空");
        $("#applyLevel").focus();
        return;
    }

    if (IsEmpty(remarks)) {
        $.ligerDialog.error("申请理由不能为空");
        $("#txtRemarks").focus();
        return;
    }

    $.ajax({
        url: "/Personnel/EditNurseLevel/Save",
        type: "post",
        data: {
            Id: Id,
            nowLevel: nowLevel,
            applyLevel: applyLevel,
            Remarks: remarks,
            Status: status
        },
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                Id = d.Body.Id;
                $("#hdnId").val(Id);
                $.ligerDialog.success("保存成功");
                setTimeout(function () { $("#btnBack").click(); }, 1000);
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function review(status) {
    $.ligerDialog.open({
        height: 300,
        width: 400,
        title: '请填写审核理由',
        url: '/Personnel/NurseLevelReview/Review',
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { id: Id, status: status },
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnBack").click();
            }

        }]
    });
    $(".l-dialog-buttons").hide();
}
